import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Friend } from 'src/entities/friend.entity';
import { User } from 'src/entities/user.entity';
import { Repository } from 'typeorm';
import { FriendDto } from './dto/friend.dto';

@Injectable()
export class FriendService {
  constructor(
    @InjectRepository(Friend)
    private friendRepository: Repository<Friend>,
  ) {}

  async findAll(userId: string) {
    let data = await this.friendRepository
      .createQueryBuilder('friend')
      .where('friend.userId= :userId', { userId: userId })
      .leftJoinAndSelect(User, 'user', 'friend.friendId=user.id')
      .select([
        'friend.friendId as id',
        'friend.isDel as isDel',
        'friend.createdTime as createdTime',
        'friend.updatedTime as updatedTime',
        'user.userName as userName',
        'user.cetName as cetName',
        'user.mobile as mobile',
        'user.eMail as eMail',
        'user.headPhoto as headPhoto',
      ])
      .getRawMany();
    return data;
  }

  async save(ageeFriendDto: FriendDto) {
    return await this.friendRepository.save(ageeFriendDto);
  }

  async findOne(userId: string,friendId: string) {
    let data = await this.friendRepository
      .createQueryBuilder('friend')
      .where('friend.userId= :userId', { userId: userId })
      .andWhere('friend.friendId= :friendId', { friendId: friendId })
      .leftJoinAndSelect(User, 'user', 'friend.friendId=user.id')
      .select([
        'friend.id as id',
        'friend.friendId as userId',
        'friend.isDel as isDel',
        'friend.createdTime as createdTime',
        'friend.updatedTime as updatedTime',
        'user.userName as userName',
        'user.cetName as cetName',
        'user.mobile as mobile',
        'user.eMail as eMail',
        'user.headPhoto as headPhoto',
      ])
      .getRawOne();
    return data;
  }
}
